package hottargui.config;

import hottargui.framework.Game;
import hottargui.framework.GameData;
import hottargui.framework.GameObjectServer;
import hottargui.framework.IllegalActionException;
import hottargui.framework.Player;
import hottargui.framework.PlayerColor;
import hottargui.framework.Position;
import hottargui.framework.State;
import hottargui.framework.TarguiState;
import hottargui.framework.TurnSequenceStrategy;
import hottargui.standard.*;

public class GammaEndRoundState implements TarguiState {

	Game gameContext;

	public GammaEndRoundState(Game game) {
		this.gameContext = game;
	}

	public boolean buy(int count, Position deploy)
			throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public State getState() {
		return State.endRound;
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public boolean move(Position from, Position to, int count)
			throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void newGame() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void rollDie() throws IllegalActionException {
		if (gameContext.getPlayerInTurn().getColor() == PlayerColor.None) {
			GameObjectServer objServer = gameContext.getGameObjectServer();
			int dieValue = objServer.getDiceRollStrategy().rollDie();
			
			System.out.println("Die thrown: " + dieValue);
			
			objServer.getGameData().setLastDieRoll(dieValue);
			TurnSequenceStrategy tss = objServer.getTurnSequenceStrategy();
			tss.startNewTurn(objServer.getGameData().getLastDieRoll());
			objServer.getGameData().setPlayerInTurn(tss.nextPlayer());
			gameContext.setState(gameContext.getFactory().getMoveState(gameContext));
		}
	}

	public PlayerColor turnCard() throws IllegalActionException {
		GameData gd = gameContext.getGameObjectServer().getGameData();
		PlayerColor retval = PlayerColor.None;
		if (gameContext.getPlayerInTurn().getColor() != PlayerColor.None) {
			System.out.println("Rounds left: " + gd.getRoundsLeft());
			
			for (Player player : gd.getPlayers().values()) {
				((StandardPlayer) player).add(gd.getRevenue(player));
			}
			
			System.out.println("Revenue added");
	
			// The Game has ended
			if (gd.getRoundsLeft() == 0) {
				System.out.println("No more rounds.");
				gameContext.setState(gameContext.getFactory().getEndGameState(gameContext));
				gd.setPlayerInTurn(PlayerColor.None); //CF 
				retval = gd.getPlayerInTurn().getColor();  	 
			} 
			else
			{ 
				// Throw dice to get the next player
				System.out.println("Starting next round");
				
				gd.decreaseRoundsLeft();
				gd.setPlayerInTurn(PlayerColor.None);
				retval = gd.getPlayerInTurn().getColor();  
			}
		}
		return retval;
	}

	public void yieldAttack() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}
	
	public void skip() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
}
